package com.huicoo.forestmanager.ui.patrol;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseFragment;
import com.huicoo.forestmanager.ui.patrol.MapFragment.MapFragment;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordFragment.PatrolRecordFragment;
import com.huicoo.forestmanager.ui.patrol.forestCampFragment.ForestCampFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PatrolFragment extends BaseFragment {

    @BindView(R.id.tv_stay)
    TextView tv_stay;
    @BindView(R.id.tv_record)
    TextView tv_record;
    @BindView(R.id.iv_transformation_style)
    ImageView iv_transformation_style;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.frame_ayout)
    FrameLayout frame_ayout;


    private List<Fragment> fragmentList = new ArrayList<>();
    private int lastFragmentIndex = -1;

    //是否是地图
    private boolean isMap=false;


    public static PatrolFragment getInstance() {
        return new PatrolFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_patrol;
    }

    @Override
    protected void initView() {

        //初始化片段
        initFragment();
        switchPage(0);
    }

    //初始化片段
    private void initFragment() {
        fragmentList.add(ForestCampFragment.getInstance());
        fragmentList.add(PatrolRecordFragment.getInstance());
        fragmentList.add(MapFragment.getInstance());
    }

    // 切换页面
    private void switchPage(int currentIndex) {
        if (lastFragmentIndex != currentIndex) {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            if (lastFragmentIndex != -1) {
                transaction.hide(fragmentList.get(lastFragmentIndex));
            }
            //地图fragment重新添加
            if (lastFragmentIndex == 2){
                transaction.remove(fragmentList.get(2));
                fragmentList.remove(2);
                fragmentList.add(MapFragment.getInstance());
            }
            if (!fragmentList.get(currentIndex).isAdded()) {
                transaction.add(R.id.frame_ayout, fragmentList.get(currentIndex));
            }
            transaction.show(fragmentList.get(currentIndex)).commitNowAllowingStateLoss();

            lastFragmentIndex = currentIndex;
        }
    }


    //待巡林班
    @OnClick(R.id.tv_stay)
    public void clickHome() {
        iv_transformation_style.setVisibility(View.VISIBLE);
        //地图样式切换
        transformationStyle(false);

        //切换片段
//        switchFragment(1);
    }


    //巡查记录
    @OnClick(R.id.tv_record)
    public void tv_record() {
        iv_transformation_style.setVisibility(View.GONE);

        //切换片段
        switchFragment(2);
    }

    //地图样式切换
    @OnClick(R.id.iv_transformation_style)
    public void iv_transformation_style() {
        //地图样式切换
        transformationStyle(true);
    }

    //地图样式切换
    private void transformationStyle(boolean whether ) {

        if (whether){
            isMap=!isMap;
        }


        //如果是地图
        if (isMap){
            //切换片段
            switchFragment(3);
            iv_transformation_style.setImageResource(R.drawable.icon_mode_list);
        }else {
            //切换片段
            switchFragment(1);
            iv_transformation_style.setImageResource(R.drawable.icon_mode_map);
        }

    }


    //切换片段
    private void switchFragment(int category) {
        if (category == 1) {
            tv_stay.setTextColor(Color.parseColor("#ffffff"));
            tv_stay.setBackgroundResource(R.drawable.fragment_patrol_head_left_049c53);

            tv_record.setTextColor(Color.parseColor("#049c53"));
            tv_record.setBackgroundResource(R.drawable.fragment_patrol_head_right_ffffff);

            // 切换页面
            switchPage(0);
        } else if (category == 2){
            tv_stay.setTextColor(Color.parseColor("#049c53"));
            tv_stay.setBackgroundResource(R.drawable.fragment_patrol_head_left_ffffff);

            tv_record.setTextColor(Color.parseColor("#ffffff"));
            tv_record.setBackgroundResource(R.drawable.fragment_patrol_head_right_049c53);

            // 切换页面
            switchPage(1);

        }else  if (category == 3) {
            tv_stay.setTextColor(Color.parseColor("#ffffff"));
            tv_stay.setBackgroundResource(R.drawable.fragment_patrol_head_left_049c53);

            tv_record.setTextColor(Color.parseColor("#049c53"));
            tv_record.setBackgroundResource(R.drawable.fragment_patrol_head_right_ffffff);


            // 切换页面
            switchPage(2);
        }


    }



}
